<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<script src="/static/admin/js/math.js"></script>
<link rel="stylesheet" type="text/css" href="/static/admin/css/main.css">
<style>
    .layui-table td, .layui-table th{text-align: center!important;padding: 0!important;height: 40px}
    .bak{background: #fafafa!important;}
    #list_list input{text-align: center}
    .colorhong{cursor: pointer;}
    .layui-form-select dl dd.layui-select-tips{padding-left: 0!important;}
    .layui-form-select dl dd{padding: 0!important;}
    #curthead tr th{font-weight: bold}
    #list_list tr td{font-size: 13px!important;}
</style>
<body class="laytp-container">
<div style="height: 45px"></div>
<div id="opt_nav">
</div>
<div id="dialog">
    <div class="page-title" id="page-title">
        <div class="page-title-con">
            <span class="title" id="titss"></span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <span id="page-close" class="page-close" onclick="layer_close()">×</span>
            </div>
        </div>
    </div>
</div>
<?php include "dao.php"?>

<div class="layui-card" id="table-card">
    <div class="layui-card-body">
        <div class="layui-row">
            <div class="layui-form layui-form-pane" id="commentForm">
                <div class="main-container" style="margin:0!important;padding: 0 10px!important;">
                    <div class="layui-form-item height_auto" style="padding: 0!important;">
                        <div class="short_table_list" key="1">
                            <div style="padding: 10px 0 5px 0;">
                                <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row insert">插入行</button>
                                <button type="button" onclick="saveinfo()" class="layui-btn layui-btn-sm btn-dark marginr5">保存</button>
                                <div class="biantit" style="color: #248bfe;font-size: 13px;float: right;margin-right: 10px"></div>
                            </div>
                            <div>
                                <table class="layui-table" lay-size="sm">
                                    <thead id="curthead">
                                    <tr>
                                        <th style="width:45px">序号</th>
                                        <th>项目名称</th>
                                        <th>单位</th>
                                        <th>建筑面积(㎡)</th>
                                        <th>层高(m)</th>
                                        <th>配模的量(m³)</th>
                                        <th>每立方支撑的用量(kg/m³)</th>
                                        <th>需要总量T</th>
                                        <th>使用时间(月)</th>
                                        <th>劳务单价(元/m³)</th>
                                        <th>轮扣租赁单价(元/T)</th>
                                        <th>不含税单价(元/m³)</th>
                                        <th>合价(元)</th>
                                        <th>折到每平方建筑面积的价格(元/㎡)</th>
                                        <th>备注</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody class="short_table_tbody" id="list_list" style="max-height: 500px"></tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-col-md12" style="margin-top: 20px">
                <div class="layui-form-item" style="margin-bottom: 50px;">
                    <label for="phone" class="layui-form-label pl">方案说明：</label>
                    <div class="layui-input-inline opt_width" style="width: 100%;margin-right: 0;">
                        <textarea name="week_lease" id="week_lease" class="layui-textarea"></textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;

        facade.ajax({url: ask_main_week_lease_list,data:{enginid:param_enginid}}).done(function(res){
            if (res.code === 2000) {
                $(".biantit").html(res.data.tit);
                $('#week_lease').val(res.data.week_lease);


                // 初始化数据
                var data = res.data.list;
                var str = ''; var all_area_c = 0;
                for(var j=0;j<data.length;j++){
                    str += '<tr class="toptr" key="'+data[j].id+'">';
                    str += '<td style="width:45px">'+getBigNum(j + 1)+'</td>';
                    str += '<td>'+data[j].qingname+'</td>';
                    str += '<td>'+data[j].area+'</td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td id="all_money"></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '</tr>';

                    if(data[j].children.length > 0){
                        var child = data[j].children;
                        for(var i=0;i<child.length;i++){
                            if(child[i].area != ''){
                                all_area_c += parseFloat(child[i].area);
                            }
                            str += '<tr>';
                            str += '<td style="width:45px">'+(i + 1)+'</td>';
                            str += '<td><select class="name">';
                            if(child[i].name == '轮扣'){
                                str += '<option value=""></option>';
                                str += '<option value="轮扣" selected>轮扣</option>';
                                str += '<option value="套扣">套扣</option>';
                                str += '<option value="盘扣">盘扣</option>';
                            }else if(child[i].name == '套扣'){
                                str += '<option value=""></option>';
                                str += '<option value="轮扣">轮扣</option>';
                                str += '<option value="套扣" selected>套扣</option>';
                                str += '<option value="盘扣">盘扣</option>';
                            }else if(child[i].name == '套扣'){
                                str += '<option value=""></option>';
                                str += '<option value="轮扣">轮扣</option>';
                                str += '<option value="套扣">套扣</option>';
                                str += '<option value="盘扣" selected>盘扣</option>';
                            }else{
                                str += '<option value=""></option>';
                                str += '<option value="轮扣">轮扣</option>';
                                str += '<option value="套扣">套扣</option>';
                                str += '<option value="盘扣">盘扣</option>';
                            }
                            str += '</select></td>';
                            str += '<td><input type="text" class="layui-input bak" readonly value="m³"></td>';
                            str += '<td><input type="number" class="layui-input area" value="'+child[i].area+'"></td>';
                            str += '<td><input type="number" class="layui-input floor_height" value="'+child[i].floor_height+'"></td>';
                            str += '<td><input type="number" class="layui-input mold bak" value="'+child[i].mold+'" readonly></td>';
                            str += '<td><input type="number" class="layui-input avg" value="'+child[i].avg+'"></td>';
                            str += '<td><input type="number" class="layui-input template bak" value="'+child[i].template+'" readonly></td>';
                            str += '<td><input type="number" class="layui-input dt" value="'+child[i].dt+'"></td>';
                            str += '<td><input type="number" class="layui-input labour_price" value="'+child[i].labour_price+'"></td>';
                            str += '<td><input type="number" class="layui-input lease_price" value="'+child[i].lease_price+'"></td>';
                            str += '<td><input type="number" class="layui-input no_price bak" value="'+child[i].no_price+'" readonly></td>';
                            str += '<td><input type="number" class="layui-input money bak" value="'+child[i].money+'" readonly></td>';
                            str += '<td><input type="number" class="layui-input pin_money bak" value="'+child[i].pin_money+'" readonly></td>';
                            str += '<td><input type="text" class="layui-input explain" value="'+child[i].explain+'"></td>';
                            str += '<td><a class="del colorhong" onclick="del(this)">删除</a></td>';
                            str += '</tr>';
                        }
                    }else{
                        str += '<tr>';
                        str += '<td style="width:45px">1</td>';
                        str += '<td><select class="name">';
                        str += '<option value=""></option>';
                        str += '<option value="轮扣">轮扣</option>';
                        str += '<option value="套扣">套扣</option>';
                        str += '<option value="盘扣">盘扣</option>';
                        str += '</select></td>';
                        str += '<td><input type="text" class="layui-input bak" readonly value="m³"></td>';
                        str += '<td><input type="number" class="layui-input area"></td>';
                        str += '<td><input type="number" class="layui-input floor_height"></td>';
                        str += '<td><input type="number" class="layui-input mold" readonly></td>';
                        str += '<td><input type="number" class="layui-input avg"></td>';
                        str += '<td><input type="number" class="layui-input template" readonly></td>';
                        str += '<td><input type="number" class="layui-input dt"></td>';
                        str += '<td><input type="number" class="layui-input labour_price"></td>';
                        str += '<td><input type="number" class="layui-input lease_price"></td>';
                        str += '<td><input type="number" class="layui-input no_price" readonly></td>';
                        str += '<td><input type="number" class="layui-input money" readonly></td>';
                        str += '<td><input type="number" class="layui-input pin_money" readonly></td>';
                        str += '<td><input type="text" class="layui-input explain"></td>';
                        str += '<td><a class="del colorhong" onclick="del(this)">删除</a></td>';
                        str += '</tr>';
                    }
                }

                str += '<tr class="heji">';
                str += '<td style="width:45px"></td>';
                str += '<td>合计</td>';
                str += '<td></td>';
                str += '<td id="all_area_c">'+all_area_c.toFixed(2)+'</td>';
                str += '<td></td>';
                str += '<td></td>';
                str += '<td></td>';
                str += '<td></td>';
                str += '<td></td>';
                str += '<td></td>';
                str += '<td></td>';
                str += '<td></td>';
                str += '<td id="all_money_c"></td>';
                str += '<td></td>';
                str += '<td></td>';
                str += '<td></td>';
                str += '</tr>';

                $('#list_list').html(str);
                jisuan();
                render_from();
                render_date();
                setTr();
            }
        });

    });

    function saveinfo(){
        var list = ['']; var qingid = 0; var exists = [];
        $('#list_list tr').each(function (i) {
            if($(this).hasClass('toptr')){
                qingid = $(this).attr('key');
                return true;
            }
            if($(this).find('#all_area_c').length){
                return true;
            }

            var name = $(this).find('.name').val();
            var area = $(this).find('.area').val();
            var floor_height = $(this).find('.floor_height').val();
            var mold = $(this).find('.mold').val();
            var avg = $(this).find('.avg').val();
            var template = $(this).find('.template').val();
            var dt = $(this).find('.dt').val();
            var labour_price = $(this).find('.labour_price').val();
            var lease_price = $(this).find('.lease_price').val();
            var no_price = $(this).find('.no_price').val();
            var money = $(this).find('.money').val();
            var pin_money = $(this).find('.pin_money').val();
            var explain = $(this).find('.explain').val();

            list.push({
                enginid:param_enginid,
                qingid:qingid,
                name:name,
                area:area,
                floor_height:floor_height,
                mold:mold,
                avg:avg,
                template:template,
                dt:dt,
                labour_price:labour_price,
                lease_price:lease_price,
                no_price:no_price,
                money:money,
                pin_money:pin_money,
                explain:explain
            });
        });
        if(list.length == 1){facade.error('请填写明细'); return false;}


        var week_lease = $('#week_lease').val();
        facade.ajax({url: ask_main_week_lease_edit, data:{enginid:param_enginid, list:list,week_lease:week_lease}}).done(function(res){
            if (res.code === 2000) {
                facade.success("提交成功!");
                setTimeout(() => {
                    window.location.reload();
                }, 1500);
            }
        });
    }









    var str_insert = '';
    str_insert += '<tr>';
    str_insert += '<td style="width:45px">1</td>';
    str_insert += '<td><select class="name">';
    str_insert += '<option value=""></option>';
    str_insert += '<option value="轮扣">轮扣</option>';
    str_insert += '<option value="套扣">套扣</option>';
    str_insert += '<option value="盘扣">盘扣</option>';
    str_insert += '</select></td>';
    str_insert += '<td><input type="text" class="layui-input bak" readonly value="m³"></td>';
    str_insert += '<td><input type="number" class="layui-input area"></td>';
    str_insert += '<td><input type="number" class="layui-input floor_height"></td>';
    str_insert += '<td><input type="number" class="layui-input mold bak" readonly></td>';
    str_insert += '<td><input type="number" class="layui-input avg"></td>';
    str_insert += '<td><input type="number" class="layui-input template bak" readonly></td>';
    str_insert += '<td><input type="number" class="layui-input dt"></td>';
    str_insert += '<td><input type="number" class="layui-input labour_price"></td>';
    str_insert += '<td><input type="number" class="layui-input lease_price"></td>';
    str_insert += '<td><input type="number" class="layui-input no_price bak" readonly></td>';
    str_insert += '<td><input type="number" class="layui-input money bak" readonly></td>';
    str_insert += '<td><input type="number" class="layui-input pin_money bak" readonly></td>';
    str_insert += '<td><input type="text" class="layui-input explain"></td>';
    str_insert += '<td><a class="del colorhong" onclick="del(this)">删除</a></td>';
    str_insert += '</tr>';




    $('.insert').click(function () {
        var choose = 1;
        $('#list_list tr').each(function () {
            if($(this).hasClass('xuanTr')){
                $(this).after(str_insert);
                choose += 1;
            }
        });
        if(choose == 1){
            layer.msg('请选择插入的位置'); return false;
        }else{



            // 循环处理序号问题
            var num = 1;
            $('#list_list tr').each(function () {
                if($(this).hasClass('toptr')){
                    num = 1;
                }else{
                    if($(this).hasClass('heji')){
                        $(this).find('td:first-child').text('');
                    }else{
                        $(this).find('td:first-child').text(num);
                        num += 1;
                    }
                }
            });
        }
        render_date();
        render_from();
        jisuan();
        setTr()
    });
    function del(obj){
        $(obj).parent().parent().remove();
        // 循环处理序号问题
        var num = 1;
        $('#list_list tr').each(function () {
            if($(this).hasClass('toptr')){
                num = 1;
            }else{
                if($(this).hasClass('heji')){
                    $(this).find('td:first-child').text('');
                }else{
                    $(this).find('td:first-child').text(num);
                    num += 1;
                }
            }
        });
        jisuan();
    }
    function setTr(){
        $('#list_list td').click(function () {
            $('#list_list tr').removeClass('xuanTr');
            $(this).parents('tr').addClass('xuanTr');
        });
    }





    $('#list_list').on('input propertychange','.area,.floor_height,.avg,.dt,.labour_price,.lease_price',function () {
        let area = $(this).parents('tr').find('.area').val() || 0;
        let floor_height = $(this).parents('tr').find('.floor_height').val() || 0;
        let avg = $(this).parents('tr').find('.avg').val() || 0;
        let dt = $(this).parents('tr').find('.dt').val() || 0;
        let labour_price = $(this).parents('tr').find('.labour_price').val() || 0;
        let lease_price = $(this).parents('tr').find('.lease_price').val() || 0;

        let mold = parseFloat(area) * parseFloat(floor_height);

        let template = 0;
        if(parseFloat(mold) * parseFloat(avg) != 0){
            template = parseFloat(mold) * parseFloat(avg) / 1000;
        }

        let money = (parseFloat(template) * parseFloat(dt) * parseFloat(lease_price)) + (parseFloat(mold) * parseFloat(labour_price));

        let no_price = 0; let pin_money = 0;
        if(money != 0){
            no_price = parseFloat(money) / parseFloat(mold);
            pin_money = parseFloat(money) / parseFloat(area)
        }

        $(this).parents('tr').find('.mold').val(mold.toFixed(2));
        $(this).parents('tr').find('.template').val(template.toFixed(2));
        $(this).parents('tr').find('.money').val(money.toFixed(2));
        $(this).parents('tr').find('.no_price').val(no_price.toFixed(2));
        $(this).parents('tr').find('.pin_money').val(pin_money.toFixed(2));

        jisuan();
    });
    function getBigNum(num){
        num = num+'';
        var s = '';
        if(parseInt(num) >= 10){
            s = '十';
        }
        var str = '';
        var arr = num.split("");
        for(var i in arr){
            var num_str = '';
            switch (arr[i]) {
                case '1': num_str = '一'; break;
                case '2': num_str = '二'; break;
                case '3': num_str = '三'; break;
                case '4': num_str = '四'; break;
                case '5': num_str = '五'; break;
                case '6': num_str = '六'; break;
                case '7': num_str = '七'; break;
                case '8': num_str = '八'; break;
                case '9': num_str = '九'; break;
            }
            if(parseInt(num) >=10 && num_str == '一'){
                continue;
            }
            if(parseInt(num) < 20 && parseInt(num) >= 10){
                str = str+s+num_str;
            }else{
                if(i == 0){
                    str = str+num_str+s;
                }else{
                    str = str+num_str;
                }
            }
        }
        return str;
    }
    function jisuan() {
        var tr_index = 0; var all_money = 0; var all_money_c = 0; var all_area_c = 0;
        $('#list_list tr').each(function (i) {
            let key = $(this).attr('key');
            if(key != undefined && key != false){
                $('#list_list tr').eq(tr_index).find('#all_money').text(all_money.toFixed(2));
                all_money_c += all_money;
                tr_index = i;
                all_money = 0;
            }else{
                let money = $(this).find('.money').val() || 0;
                all_money += parseFloat(money);

                let area = $(this).find('.area').val() || 0;
                all_area_c += parseFloat(area);
            }
        });
        $('#list_list tr').eq(tr_index).find('#all_money').text(all_money.toFixed(2));
        all_money_c += all_money;

        $('#all_money_c').text(all_money_c.toFixed(2));
        $('#all_area_c').text(all_area_c.toFixed(2));
    }
</script>
</body>
</html>
